How to load stored settings from an Options page inside of a plug-in
This is the third and the latest part of a topic about adding an options page into your DXCore plug-in. See the other parts, to learn more:
- Adding and designing an options page
- Implementing the options page settings storing logic
- Using settings from an options page inside a plug-in (this post)
In this post, we are going to read plug-in settings from the decoupled storage and update them when they are changed on the options page.
In the Visual Studio form designer, activate your standard plug-in design surface. In the Properties tool window, click the Events button to see the available events. Double-click the OptionsChanged event to create an event handler for it.
The OptionsChanged event is fired when the user clicks either the OK or the Apply button on the IDETools Options dialog (this event is triggered after your custom options page handles its CommitChanges event).
Inside the OptionsChanged event handler, add the following code to see if your page was changed:
private void PlugIn1_OptionsChanged(OptionsChangedEventArgs ea) { if (ea.OptionsPages.Contains(typeof(OptMySettingsPage))) UpdateSettings(); }
Private Sub PlugIn1_OptionsChanged(ByVal ea As OptionsChangedEventArgs) Handles MyBase.OptionsChanged If ea.OptionsPages.Contains(GetType(OptMySettingsPage)) Then UpdateSettings() End If End Sub
Change “OptMySettingsPage” in this sample code to the class name of your options page. Now, it’s time to implement the UpdateSettings method. It may look like this:
void UpdateSettings() { using (DecoupledStorage storage = OptMySettingsPage.Storage) { _IsEnabled = storage.ReadBoolean("Preferences", "Enabled", true); // ... } }
Sub UpdateSettings() Using storage As DecoupledStorage = OptMySettingsPage.Storage _IsEnabled = storage.ReadBoolean("Preferences", "Enabled", True) ' ... End Using End Sub
_IsEnabled is a private boolean field that can be declared in this plug-in to hold the state of the option. Replace the “…” comment with similar assignments to other private fields declared in your plug-in.
Make ensure that the default values passed to ReadBoolean and other ReadXxx methods of the DecoupledStorage object match the default values specified in your PreparePage event handler, and in your RestoreDefaults event handler. Since these default values are showing up in three places, you might want to make them public static or constant fields. This is especially useful if you expect your default values to be changed often during development.
The final step is to initialize settings according to the stored state (or set them to defaults). The easiest way is to do this on a plug-in startup – just add a call to “UpdateSettings” at the end of the “InitializePlugIn” method of the standard plug-in instance.
// DXCore-generated code... #region InitializePlugIn public override void InitializePlugIn() { base.InitializePlugIn(); UpdateSettings(); } #endregion
'DXCore-generated code... #Region " InitializePlugIn " Public Overrides Sub InitializePlugIn() MyBase.InitializePlugIn() UpdateSettings() End Sub #End Region
That’s it – now you have a custom options page for you plug-in!
—– Products: DXCore Versions: 10.2 and up VS IDEs: any Updated: Feb/28/2011 ID: D067